import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
import { query, logout } from 'services/app'
import * as menuService from 'services/menu'
import { infoDetais } from 'services/search'
import { userLis,searchs,remove } from 'services/search'
import { applicationSave1,applicationUpdate1,getGzcFileList1 } from 'services/fileComponent'

const { prefix } = config
const permissions = JSON.parse(sessionStorage.getItem("permissions"))

export default {
  namespace: 'fileDestroy',
  state: {
	list:'',
	list1:'',
	list2:'',
	list3:'',
	isNew:'',
	visible:false,
	menu:permissions.indexOf("file:fileDestroy:apply") != -1 ? 0:1,  //判断权限如果是有申请权限则key为0，无则key为1
	details:'',
	userList:'',
	params:'',
	info:'',
	showDetails:false,
	pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },	
  },
  subscriptions: {
	setup({dispatch,history}){
    		history.listen((location)=>{
    			const { query, pathname } = location;
    			if(pathname === "/file/fileDestroy"){
    				dispatch({
    					type:'useLists',
    					payload:location.query,
    				})			
    			}
    			
    		})
    }
  },
  effects: {
  	 *applicationSave({payload},{call,put}){
  	 	const data = yield call (applicationSave1,payload)
  	 	yield put({type:'showLoading'})
  	 	if(data.success){
  	 		yield put({ type: 'reload' });
  	 	}else{
  	 		yield put({type:'hideLoading'})
  	 	}
  	 },
  	 *applicationUpdate({payload},{call,put}){
  	 	const data = yield call (applicationUpdate1,payload)
  	 	
  	 	yield put({type:'showLoading'})
  	 	if(data.success){
  	 		yield put({ type: 'reload' });
  	 		yield put({
				type:'showDetailSuccess',
				payload:{
					showDetaisl:false
				}
			})
  	 	}else{
  	 		yield put({type:'hideLoading'})
  	 	}
  	 },  
	*showPage({payload},{call,put}){
		const data = yield call (infoDetais, payload)
		yield put({
			type:'showPageSuccess',
			payload:{
				showDetails:true,
				info:data
			}
		})
	},
  	*'delete' ({ payload }, { call, put }) {
	      yield put({ type: 'showLoading' })
	      const data = yield call(remove, payload )
	      if (data.success) {
	        yield put({ type: 'reload' });
	        yield put({ type: 'showList' })
	      }else{
	        yield put({ type: 'hideLoading' })
	      }
    },	
	*showList({payload},{call,put}){
		yield put({
			type:'hidePageSuccess',
			payload:{
				showDetails:false,
			}
		})
	},
  	 *reload(action, { put, select }) {
	  const page = yield select(({ fileDestroy }) => fileDestroy.pagination)
	  const menu = yield select(({ fileDestroy }) => fileDestroy.menu)
      yield put({ type: 'searchList', payload: { 
          page:page.current,
          operatingType:2,
          operatingState:menu,
       } });
    },
  	// *reloadss(action, { put, select }) {
    //   const page = yield select(({ fileDestroy }) => fileDestroy.pagination1)
    //   yield put({ type: 'searchList', payload: { 
    //       page:page.current,
    //       operatingType:2,
    //       operatingState:0,
    //    } });
    // },    
  	// *reloads(action, { put, select }) {
    //   const page = yield select(({ fileDestroy }) => fileDestroy.pagination1)
    //   yield put({ type: 'searchList', payload: { 
    //       page:page.current,
    //       operatingType:2,
    //       operatingState:1,
    //    } });
    // },    
    *useLists({payload},{call,put}){
    		const data = yield call (userLis,payload)
    		if(data.success){
    			yield put({
    				type:'querySuccess3',
    				payload:{
    					userList:data.userList
    				}
    			})
    		}
    },  	
  	 *showDet({payload},{call,put}){
		const data = yield call (infoDetais, payload)
		yield put({
			type:'showDetailSuccess',
			payload:{
				details:data
			}
		})
	},
  	 *searchList({payload},{call,put}){
    		const data = yield call (getGzcFileList1,payload)
		//0为申请 1为审批
    		if(data.success){
			yield put({
    				type:'querySuccess2',
    				payload:{
    					params:payload,
    					list:data.page.list,
    					pagination:{
    						total: data.page.totalCount,
            				current: data.page.currPage
    					}
    				}
    			})
    		}
    },  	
  	*onModalOk({payload},{call,put}){
  		yield put({ type: 'showLoading' })
  		yield put({
  			type:'querySuccess',
  			payload:{
  				visible:false,
  			}
  		})
  	},
	*showModal({payload},{call,put}){
		yield put({
			type:'showModalSuccess',
			payload:{
				visible:true,
  				isNew:payload.name
			}
		})
	},
	*onCancels({payload},{call,put}){
		yield put({
			type:'showModalSuccess',
			payload:{
				visible:false
			}
		})
	},
	*showMenu({payload},{call,put}){
		yield put({
			type:'querySuccess1',
			payload:{
				menu:payload
			}
		})
	},
  },
  reducers: {
 	showDetailSuccess(state,action){
		const {details} = action.payload
		return {
			...state,
			details
		}
	}, 	
  	showModalSuccess(state,action){
  		const {visible,isNew} = action.payload
  		return{
  			...state,
  			isNew,
  			visible
  		}
  	},
  	querySuccess(state,action){
  		const {visible} = action.payload	
  		return{
  			...state,
  			visible
  		}
  	},
  	querySuccess1(state,action){
  		const {menu} = action.payload
  		return{
  			...state,
  			menu
  		}
  	},
  	querySuccess2(state,action){
  		const {list,pagination,params} = action.payload
    		return {...state,
    			list,
    			params,
    			pagination: {
	          ...state.pagination,
	          ...pagination
	        },
    		}	
  	},
	querySuccess3(state,action){
    		const {userList} = action.payload
    		return {...state,
    			userList
    		}
    },  	
  	showLoading(state){
  		return{
  			...state,
  			loading:true
  		}
  	},
    hideLoading (state) {
      return { 
      	...state,
      	loading: false
      }
    },
	showPageSuccess(state,action){
		const {showDetails,info} = action.payload
		return {
			...state,
			showDetails,
			info
		}
	},  	
	hidePageSuccess(state,action){
		const {showDetails} = action.payload
		return {
			...state,
			showDetails
		}
	}
  },
}
